package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/page")
    public Result findUserByPage(QueryPageBean queryPageBean){
        try {
                PageResult pageResult = userService.findByPage(queryPageBean);
                return new Result(true, MessageConstant.QUERY_USER_SUCCESS, pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_USER_FAIL);
        }
    }
    //查询所有角色
    @GetMapping("/role/all")
    public Result findAllRole(){
        try {
            List<Role> roles = userService.findAllRole();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roles);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //新增角色
    @PostMapping
    public Result addUser(@RequestBody User user,@RequestParam Integer[] roleIds){
        try {
            userService.insert(user,roleIds);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }
    /*根据id查询角色*/
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
       User user =  userService.findById(id);
       if (user!=null){
           return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
       }
       return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }
    //编辑角色
    @PutMapping()
    public Result updateUser(@RequestBody User user,
                             @RequestParam("roleIds") Integer[] roleIds){
        try {
            userService.update(user,roleIds);
            return new Result(true,MessageConstant.UPDATE_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_USER_FAIL);
        }
    }
    //查询检查组关联的检查项信息
    @GetMapping("/{id}/roleIds")
    public Result findMenus(@PathVariable("id") Integer id){
        try {
            Integer[] ids =  userService.findRoleById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,ids);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    /**
     * 删除用户
     *
     * @param id
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    @GetMapping("/getUsername")
    public Result getUsername(){
        try {
            String name =  SecurityContextHolder.getContext().getAuthentication().getName();
            return new Result(true,"查询用户名成功",name);
            //return new Result(false,"查询用户名成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户名失败");
        }
    }

    @GetMapping("/getMenu/{username}")
    public Result getMenu(@PathVariable("username") String username){
        try {
            List<Menu> menuList = userService.getMenu(username);
            return new Result(true,"查询菜单成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }


}
